layui.use('table', function () {

    var table = layui.table;
    //集合名称
    var checkScenarioName = $("#scheme").val();
    var sampleKey = $("#sampleKey").val();
    var quarantineQuantity = $("[name=quarantineQuantity]").val();
    var receiptKey = $("[name=receiptKey]").val();
    var sku = $("[name=sku]").val();
    var qualityStatus =  $("[name=qualityStatus]").val();
    var checkItemFlag =  $("[name=checkItemFlag]").val();

    //检验明细
    table.render({
        elem: '#detectionDetailList'
        , url: '/3nod_web/iqcDetectionDetail/list/'
        // , cellMinWidth: 0
        , cols: [
            [
                {field: 'receiptKey', title: 'ASN/收货单', sort: true, fixed: true}
                // , {field: 'shipper', title: '货主'}
                // , {field: 'sku', title: '物料编码'}
                , {field: 'dateCode', title: 'DateCode'}
                // , {field: 'supplierKey', title: '供应商代码'}
                , {field: 'receivedQty', title: '接收量'}
                , {field: 'quarantineQuantity', title: '待检数量'}
                , {field: 'unqualifiedQuantity', title: '不合格数量', edit: 'text'}
                , {field: 'sampleQuantity', title: '留样数量', edit: 'text'}
                , {field: 'rejectionQuantity', title: '拒收数量', edit: 'text'}
                , {field: 'inventoryQuantity', title: '入库数量'}
                , {field: 'checkBackQuantity', title: '验退数量'}
                , {field: 'lottable03', title: 'lottable03'}
                , {field: 'editWho', title: '修改人'}
                , {field: 'editDate', title: '修改日期'}
            ]
        ]
        , where: {receiptKey: receiptKey, sku: sku}
        , id: 'detectionDetailReload'
        , page: true
        , limits: [20, 50, 100, 200, 500, 1000, 2000, 3000, 4000, 5000]
        , limit: 20
        // , height: 'full-400'
        , height: 300
        ,done:function (res, curr, count) {
            let tableView = this.elem.next();
            layui.each(res.data, function(i, item) {
                if (qualityStatus=='3' ){
                    tableView.find('tr[data-index=' + i + ']').find('td[data-field!=unqualifiedQuantity]').data('edit', false);
                }
            })
        }
    });


    //检验项目
    table.render({
        elem: '#demo'
        , url: '/3nod_web/iqcCheckItemDetails/checkItemDetails/'
        , cols: [
            [
                {field: 'checkProject', title: '检验项目', sort: true, fixed: true, width: 200}
                , {field: 'checkStandard', title: '描述', edit: 'text', width: 550}
                , {field: 'criticalDefect', title: '致命缺陷', edit: 'text',width: 86}
                , {field: 'seriousDefects', title: '严重缺陷', edit: 'text',width: 86}
                , {field: 'lightDefects', title: '轻微缺陷', edit: 'text',width: 86}
                // , {
                // field: 'lightDefects',
                // title: '轻微缺陷',
                // templet: function (d) {
                //     // 模板的实现方式也是多种多样，这里简单返回固定的
                //     return '<input name="lightDefects"  type="radio" value="OK" />OK </label> ' +
                //         '<input name="lightDefects" type="radio" value="NG" />NG </label> ';
                //     }
                //  }
                , {field: 'numberOfDefectiveSa', title: '不良数', edit: 'text',width: 73}
                , {field: 'remark', title: '描述', edit: 'text',width: 450}
            ]
        ]
        ,done:function (res, curr, count) {
            let tableView = this.elem.next();
            layui.each(res.data, function(i, item) {
                //  如果质检状态是完成，并且检验项目状态也是完成变成只读
                if (qualityStatus=='3' && checkItemFlag=='Y' ){
                    tableView.find('tr[data-index=' + i + ']').find('td').data('edit', false);
                }
            })
        }
        , where: {checkScenarioName: checkScenarioName, receiptKey: receiptKey, sku: sku}
        , id: 'testReload'
        // , height: 'full-400'
        , height: 300
    });
    //AQL
    table.render({
        elem: '#demo2'
        , url: '/3nod_web/iqcSamplingShow/sampleShow?quarantineQuantity=' + quarantineQuantity
        , cols: [
            [
                {templet: '#xuhao', title: '序号', width: 65, fixed: true}
                , {field: 'defectLevels', title: '缺陷等级', width: 100, fixed: true}
                , {field: 'aqlStandard', title: 'AQL标准', width: 120}
                , {field: 'acceptanceNumber', title: '允收数', width: 80}
                , {field: 'rejectionNumber', title: '拒收数', width: 80}
                , {field: 'rejectedNumber', title: '不良数', width: 80}
            ]
        ]
        , where: {sampleKey: sampleKey, checkScenarioName: checkScenarioName, sku: sku, receiptKey: receiptKey}
        , id: 'testReload2'
        , height: 200
        , done: function (res, curr, count) {
            iqcData = res.data;
            console.log(iqcData[0].sampleQty)
            //修改抽样数
            $("[name=samplingQuantity]").val(iqcData[0].sampleQty);
        }
    });

    //检验项目编辑
    table.on('edit(checkItem)', function (obj) {
        var $ = layui.$;
        var value = obj.value //得到修改后的值
            , data = obj.data;//得到所在行所有键值
        $.ajax({
            url: "/3nod_web/iqcCheckItemDetails/update", // ajax请求路径
            type: 'POST',
            contentType: 'application/json; charset=UTF-8',
            async: false,
            dataType: 'text',
            data: JSON.stringify(data),
            success: function (data) {
                if (data == '请求超时，请点击左侧菜单，重新载入!') {
                    layer.alert(data);
                    return false;
                }
                if (data == '合格') {
                    $('#decideWay1').css('display', 'none');
                    $('#decideWay2').css('display', 'block');
                } else {
                    $('#decideWay1').css('display', 'block');
                    $('#decideWay2').css('display', 'none');
                }
                //改变检验结果的值
                $("[name=checkResult]").val(data);
                // $("[name=decideWay]").val(data);
                if (data == '不合格') {
                    var select = 'dd[lay-value=' + 1 + ']';
                    $('#decideWay').siblings("div.layui-form-select").find('dl').find(select).click();
                }
            },
            error: function () {
                layer.alert("更新失败");
            }
        });
        testReload4();
    });
    //检验明细编辑
    table.on('edit(detectionDetailListLF)', function (obj) {

        var $ = layui.$;
        var decideWay2 = $("#decideWay2").css('display');
        var value = obj.value //得到修改后的值
            , data = obj.data;//得到所在行所有键值
        var receivedQty = data.receivedQty; //接收量
        var unqualifiedQuantity = data.unqualifiedQuantity; //不合格数量
        var sampleQuantity = data.sampleQuantity;//留样数量
        var rejectionQuantity = data.rejectionQuantity; //拒收数量
        var inventoryQuantity = data.inventoryQuantity; //入库数量
        var samplingQuantity = $("[name=samplingQuantity]").val();

        //获取编辑前的值
        var old = $(this).prev().text();//旧值

        if (isNaN(value)) {
            layer.alert('请输入数字');
        } else if (decideWay2 !== 'none') {
            if (inventoryQuantity < 1) {
                layer.alert('入库数量必须大于0');
            }
        } else if (parseInt(unqualifiedQuantity) > parseInt(samplingQuantity)) {
            layer.alert('不合格数量不能大于抽检数量');
        } else if (parseInt(value) < 0) {
            layer.alert('不能输入小于0的数');
        } else if (parseInt(value) > parseInt(receivedQty)) {
            layer.alert("不能大于接收量");
            // $(this).val(old);//重新掰回来
        } else if (parseInt(sampleQuantity) + parseInt(rejectionQuantity) > parseInt(receivedQty)) {
            layer.alert("留样数量+拒收数量 不能大于接收量");
            // $(this).val(old);//重新掰回来
        }
        else {
            $.ajax({
                url: "/3nod_web/iqcDetectionDetail/update", // ajax请求路径
                type: 'POST',
                contentType: 'application/json; charset=UTF-8',
                async: false,
                dataType: 'json',
                data: JSON.stringify(data),
                success: function (data) {
                    if (data == '请求超时，请点击左侧菜单，重新载入!') {
                        layer.alert(data);
                        return false;
                    }
                    $("[name=unqualifiedQuantity]").val(data.unqualifiedQuantity);
                    $("[name=quarantineQuantity]").val(data.quarantineQuantity);
                    $("[name=sampleQuantity]").val(data.sampleQuantity);
                    $("[name=rejectionQuantity]").val(data.rejectionQuantity);
                    $("[name=checkBackQuantity]").val(parseInt($("[name=sampleQuantity]").val()) + parseInt($("[name=rejectionQuantity]").val()));
                    $("[name=inventoryQuantity]").val($("[name=quarantineQuantity]").val() - $("[name=rejectionQuantity]").val() - $("[name=sampleQuantity]").val());
                },
                error: function () {
                    layer.alert("更新失败");
                }
            });

            var unqualifiedQuantity = $("[name=unqualifiedQuantity]").val();
            var samplingQuantity = $("[name=samplingQuantity]").val();
            $("[name=conformityQuantity]").val(samplingQuantity - unqualifiedQuantity);
            //重载当前表格
            detectionDetailReload();
            //更新不良率
            updateConformityQuantity();
        }
    });
});